package helpers;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ParsingHelper {
	public static StringBuilder parseBetween( StringBuffer stringBuffer, String from, String to ) {
		int fromIndex = stringBuffer.indexOf( from );
		int toIndex = stringBuffer.indexOf( to, fromIndex );

		if ( fromIndex == 0 || toIndex == 0 ) {
			throw new StringIndexOutOfBoundsException( "From was " + fromIndex + " to was " + toIndex );
		}

		return new StringBuilder( stringBuffer.substring( fromIndex, toIndex ) );
	}

	public static String findGroup( String regex, CharSequence content, int groupNumber ) {
		Pattern pattern = Pattern.compile( regex );

		Matcher matcher = pattern.matcher( content );

		if ( matcher.find() == true ) {
			return matcher.group( groupNumber );
		} else {
			return "";
		}
	}
}
